package com.zhangxu.microservice.auth.controller.ae;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhangxu.microservice.auth.biz.dto.ResourceDTO;
import com.zhangxu.microservice.auth.biz.dto.UpdateResourceDTO;
import com.zhangxu.microservice.auth.biz.service.ResourceService;
import com.zhangxu.microservice.auth.dao.condition.ResourceCondition;
import com.zhangxu.microservice.auth.mapstruct.ResourceWebConverter;
import com.zhangxu.microservice.auth.vo.req.CreateResourceReq;
import com.zhangxu.microservice.auth.vo.req.PageReq;
import com.zhangxu.microservice.auth.vo.req.ResourceQueryReq;
import com.zhangxu.microservice.auth.vo.req.UpdateResourceReq;
import com.zhangxu.microservice.auth.vo.resp.PageResponse;
import com.zhangxu.microservice.auth.vo.resp.ResourceResp;
import com.zhangxu.microservice.common.result.CommonResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 资源管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/ae/resources")
@RequiredArgsConstructor
@Tag(name = "资源管理", description = "资源管理相关接口")
public class ResourceController {

    private final ResourceService resourceService;
    private final ResourceWebConverter resourceWebConverter;

    @Operation(summary = "创建资源", description = "创建新资源")
    @PostMapping
    public CommonResult<Long> createResource(@RequestBody @Validated CreateResourceReq createResourceReq) {
        Long resourceId = resourceService.createResource(resourceWebConverter.toCreateResourceDTO(createResourceReq));
        return CommonResult.success(resourceId);
    }

    @Operation(summary = "更新资源", description = "更新现有资源信息")
    @PutMapping("/{resourceId}")
    public CommonResult<Void> updateResource(
            @Parameter(description = "资源ID", required = true, example = "1") 
            @PathVariable("resourceId") Long resourceId,
            @RequestBody @Validated UpdateResourceReq updateResourceReq) {
        UpdateResourceDTO updateResourceDTO = resourceWebConverter.toUpdateResourceDTO(updateResourceReq);
        updateResourceDTO.setId(resourceId);
        resourceService.updateResource(updateResourceDTO);
        return CommonResult.success();
    }

    @Operation(summary = "删除资源", description = "根据ID删除资源")
    @DeleteMapping("/{resourceId}")
    public CommonResult<Void> deleteResource(
            @Parameter(description = "资源ID", required = true, example = "1")
            @PathVariable("resourceId") Long resourceId) {
        resourceService.deleteResource(resourceId);
        return CommonResult.success();
    }

    @Operation(summary = "启用资源", description = "启用指定资源")
    @PostMapping("/{resourceId}/enable")
    public CommonResult<Void> enableResource(
            @Parameter(description = "资源ID", required = true, example = "1")
            @PathVariable("resourceId") Long resourceId) {
        resourceService.enableResource(resourceId);
        return CommonResult.success();
    }

    @Operation(summary = "禁用资源", description = "禁用指定资源")
    @PostMapping("/{resourceId}/disable")
    public CommonResult<Void> disableResource(
            @Parameter(description = "资源ID", required = true, example = "1")
            @PathVariable("resourceId") Long resourceId) {
        resourceService.disableResource(resourceId);
        return CommonResult.success();
    }

    @Operation(summary = "获取资源详情", description = "根据ID获取资源详情")
    @GetMapping("/{resourceId}")
    public CommonResult<ResourceResp> getResourceById(
            @Parameter(description = "资源ID", required = true, example = "1")
            @PathVariable("resourceId") Long resourceId) {
        ResourceDTO resourceDTO = resourceService.getResourceById(resourceId);
        return CommonResult.success(resourceWebConverter.toResourceResp(resourceDTO));
    }

    @Operation(summary = "根据父ID获取子资源列表", description = "获取指定父资源下的所有子资源")
    @GetMapping("/parent/{parentId}")
    public CommonResult<List<ResourceResp>> getResourcesByParentId(
            @Parameter(description = "父资源ID", required = true, example = "1")
            @PathVariable("parentId") Long parentId) {
        List<ResourceDTO> resourceDTOList = resourceService.getResourcesByParentId(parentId);
        return CommonResult.success(resourceDTOList.stream()
                .map(resourceWebConverter::toResourceResp)
                .toList());
    }

    @Operation(summary = "资源列表", description = "分页查询资源列表")
    @GetMapping
    public CommonResult<PageResponse<ResourceResp>> resourcePage(
            @Valid PageReq pageReq,
            @Valid ResourceQueryReq queryReq) {
        
        ResourceCondition condition = new ResourceCondition();
        condition.setName(queryReq.getName());
        condition.setCode(queryReq.getCode());
        condition.setType(queryReq.getType());
        condition.setSystemId(queryReq.getSystemId());
        condition.setParentId(queryReq.getParentId());
        condition.setStatus(queryReq.getStatus());
        condition.setVisible(queryReq.getVisible());
        condition.setPageNum(pageReq.getPageNum());
        condition.setPageSize(pageReq.getPageSize());

        IPage<ResourceDTO> page = resourceService.resourcePage(condition);
        return CommonResult.success(PageResponse.convert(page, resourceWebConverter::toResourceResp));
    }
}